/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: win_atomic.c
 *
 * Purpose: virtual os layer atomic operation interface windows implement 
 *
 * Developer:
 *   wen.gu , 2015-10-22
 *
 * TODO: 
 *
 ***************************************************************************/


//inner header file
#include "sys_config.h"

#if defined(G_OS_WINDOWS)
//API header file
#include "vos_atomic.h"
#include <windows.h>
#include <process.h>
//#include <winbase.h>

/***************************************************************************
*
* windows atomic operation API define
*
***************************************************************************/
/**
*
*@brief  add a value to orgValue as atomic operation
*
*@param  orgValue [io] origin value.
*@param  addValue [in] add value.
*
*@return previous *orgValue.
*
*@see
*/
GS32 vosAtomicExchangeAdd(GS32 volatile *orgValue, GS32 addValue)
{
    return InterlockedExchangeAdd((_Inout_ LONG volatile*)orgValue, addValue);
}

/**
*
*@brief  add 1 to orgValue as atomic operation
*
*@param  orgValue [io] origin value.
*
*@return  previous *orgValue.
*
*@see
*/
GS32 vosAtomicIncrement(GS32 volatile *orgValue)
{
    //return InterlockedIncrement(orgValue);
    return InterlockedExchangeAdd((_Inout_ LONG volatile*)orgValue, 1);
}

/**
*
*@brief  dec  1 to orgValue as atomic operation
*
*@param  orgValue [io] origin value.
*
*@return previous *orgValue.
*
*@see
*/
GS32 vosAtomicDecrement(GS32 volatile *orgValue)
{
   // return InterlockedDecrement(orgValue);
    return InterlockedExchangeAdd((_Inout_ LONG volatile*)orgValue, -1);
}

/**
*
*@brief  atomic 'or' operation
*
*@param  orgValue [io] origin value.
*
*@return previous orgValue.
*
*@see
*/
GS32 vosAtomicOr(GS32 volatile *orgValue, GS32 value)
{
    return InterlockedOr((_Inout_ LONG volatile*)orgValue, value);
}

/**
*
*@brief  atomic 'or' operation
*
*@param  orgValue [io] origin value.
*
*@return previous orgValue.
*
*@see
*/
GS32 vosAtomicAnd(GS32 volatile *orgValue, GS32 value)
{
    //return InterlockedAnd(orgValue, value);
    return _InterlockedAnd((_Inout_ LONG volatile*)orgValue, value);
}

/**
 *
 *@brief  compare orgValue with old_value, if equal then orgValue with new_value
 *
 *@param  orgValue  [io] origin value.
 *        old_value [in] old value.
 *        new_value [in] new value.
 *
 *@return This returns true(value is 1) if the new value was successfully stored, 
 *        which will only happen when *orgValue == old_value.
 *
 *@see
 */
GS32 vosAtomicCmpExchange(GS32 volatile *orgValue, GS32 old_value, GS32 new_value)
{
   // GS32 temp = *orgValue;
    return old_value == InterlockedCompareExchangeRelease((_Inout_ LONG volatile*)orgValue, new_value, old_value);

    //return temp != old_value;
}

//////////////////////////////////////////////////////////////////////////

/**
*
*@brief  add a value to orgValue as atomic operation(orgValue is 64 bit)
*
*@param  orgValue [io] origin value.
*@param  addValue [in] add value.
*
*@return previous *orgValue.
*
*@see
*/
GS64 vosAtomicExchangeAdd64(GS64 volatile *orgValue, GS64 addValue)
{
    return InterlockedExchangeAdd64((_Inout_ LONGLONG volatile*)orgValue, addValue);
}

/**
*
*@brief  add 1 to orgValue as atomic operation(orgValue is 64 bit)
*
*@param  orgValue [io] origin value.
*
*@return previous *orgValue.
*
*@see
*/
GS64 vosAtomicIncrement64(GS64 volatile *orgValue)
{
   // return InterlockedIncrement64(orgValue);
    return InterlockedExchangeAdd64((_Inout_ LONGLONG volatile*)orgValue, 1);
}

/**
*
*@brief  dec  1 to orgValue as atomic operation(orgValue is 64 bit)
*
*@param  orgValue [io] origin value.
*
*@return previous *orgValue.
*
*@see
*/
GS64 vosAtomicDecrement64(GS64 volatile *orgValue)
{
    //return InterlockedDecrement64(orgValue);
    return InterlockedExchangeAdd64((_Inout_ LONGLONG volatile*)orgValue, -1);
}

#endif //end of defined(G_OS_WINDOWS)
